package com.caverock.androidsvgsample.fragments;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.caverock.androidsvg.SVG;
import com.caverock.androidsvgsample.R;

import java.util.Random;

/**
 * Demonstrates how to render an SVG to a Bitmap object user SVG.renderToCanvas().
 *
 * For the SVG we are using the "Bouquet" emoji from the Google Noto Emoji font.
 * https://github.com/googlei18n/noto-emoji/blob/master/svg/emoji_u1f490.svg
 */
public class RenderToImageFragment extends Fragment
{
   private static final String  TAG = RenderToImageFragment.class.getSimpleName();

   private static final int    WIDTH = 500;
   private static final int    HEIGHT = 500;
   private static final int    NUM_BOUQUETS = 10;
   private static final float  SCALE_MIN = 0.1f;
   private static final float  SCALE_MAX = 0.5f;

   SVG  bouquet = null;

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
   {
       return inflater.inflate(R.layout.content_single_image, container, false);
   }


   @Override
   public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
   {
      final TextView   titleView  = view.findViewById(R.id.title);
      final ImageView  imageView = view.findViewById(R.id.image);

      titleView.setText(R.string.title_render_to_image);

      // Load the SVG from the assets folder
      try {
         bouquet = SVG.getFromAsset(getActivity().getAssets(), "ye.svg");
      } catch (Exception e) {
         Log.e(TAG, "Could not load flower emoji SVG", e);
      }

      // Se tab listener
      imageView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v)
         {
            Bitmap  generatedImage = generateImage();
            imageView.setImageBitmap(generatedImage);
         }
      });

      //      Bitmap  generatedImage = generateImage();
//      imageView.setImageBitmap(generatedImage);
      // 假设有一个包含要生成图像的ImageView的资源ID的数组
      int[] imageViewIds = {R.id.image, R.id.image2, R.id.image3, R.id.image4, R.id.image5, R.id.image6, R.id.image7, R.id.image8,
              R.id.image9, R.id.image10, R.id.image11, R.id.image12,
              R.id.image13, R.id.image14, R.id.image15, R.id.image16,
              R.id.image17, R.id.image18, R.id.image19, R.id.image20,
              R.id.image21, R.id.image22, R.id.image23};
//      int[] imageViewIds = {R.id.image};
//
// 循环遍历数组，为每个ImageView生成并设置Bitmap
//      for (int imageViewId : imageViewIds) {
//         // 生成图像
//         Bitmap generatedImage = generateImage();
//
//         // 找到对应的ImageView
//         ImageView imageView2 = view.findViewById(imageViewId);
//
//         // 设置Bitmap
//         imageView2.setImageBitmap(generatedImage);
//      }

// 使用 Handler 在主线程更新 UI
      Handler handler = new Handler(Looper.getMainLooper());

      for (int imageViewId : imageViewIds) {


         // 使用线程池执行任务
         new Thread(() -> {
            // 记录开始时间
            long startTime = System.currentTimeMillis();
            // 生成图像
            Bitmap generatedImage = myGenerateImage();

            // 记录结束时间
            long endTime = System.currentTimeMillis();

            // 计算耗时
            long elapsedTime = endTime - startTime;

            // 打印日志
            Log.d("YourTag", "生成图像耗时: " + elapsedTime + " 毫秒");

            // 在主线程更新 UI
            handler.post(() -> {
               // 找到对应的ImageView
               ImageView imageView2 = view.findViewById(imageViewId);

               // 设置Bitmap
               imageView2.setImageBitmap(generatedImage);
            });
         }).start();
      }

   }


   private Bitmap  generateImage()
   {
      // Create a Bitmap to render our SVG to
      Bitmap  bm = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
      // Create a Canvas to use for rendering
      Canvas canvas = new Canvas(bm);

      // Clear the background to white
      canvas.drawRGB(255, 255, 255);

      Random  random = new Random();

      for (int i=0; i<NUM_BOUQUETS; i++)
      {
         // Save the canvas state (because we are going to alter the transformation matrix)
         canvas.save();

         // Choose a random scale
         // If we don't specify a viewport box, then AndroidSVG will use the bounds of the Canvas
         // as the viewport.  So a scale of 1.0 corresponds to that size.
         // A scale of 0.1 would draw the SVG at 1/10th of the size of the Canvas/Bitmap.
         float scale = SCALE_MIN + random.nextFloat() * (SCALE_MAX - SCALE_MIN);

         // Choose a random position
         float  x = random.nextFloat();   // 0.0 -> 1.0
         float  y = random.nextFloat();

         // If we just draw the SVG at x,y, then it will be drawn below and to the right of that.
         // But we want to centre at x,y instead. We can do that by subtracting half the scale.
         x -= scale / 2;
         y -= scale / 2;

         canvas.translate(x * WIDTH, y * HEIGHT);
         canvas.scale(scale, scale);

         // Now render the SVG to the Canvas
         bouquet.renderToCanvas(canvas);

         // And restore the Canvas's matrix to what it was before we altered it
         canvas.restore();
      }

      return bm;
   }


   private Bitmap myGenerateImage() {
      // Create a Bitmap to render our SVG to
      Bitmap bm = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);

      // Create a Canvas to use for rendering
      Canvas canvas = new Canvas(bm);

      // Clear the background to white
      canvas.drawRGB(255, 255, 255);

      // Save the canvas state (because we are going to alter the transformation matrix)
      canvas.save();

      // Choose a scale (you can adjust this based on your requirements)
      float scale = 1.0f;

      // Choose a position (you can adjust this based on your requirements)
      float x = 0.5f;  // Centered horizontally
      float y = 0.5f;  // Centered vertically

      // If we just draw the SVG at x,y, then it will be drawn below and to the right of that.
      // But we want to center at x,y instead. We can do that by subtracting half the scale.
      x -= scale / 2;
      y -= scale / 2;

      // Translate and scale the canvas
      canvas.translate(x * bm.getWidth(), y * bm.getHeight());
      canvas.scale(scale, scale);

      // Now render the SVG to the Canvas
      bouquet.renderToCanvas(canvas);

      // Restore the Canvas's matrix to what it was before we altered it
      canvas.restore();

      return bm;
   }


}
